import psutil
import time
import datetime

class SystemMonitoring(object):
    
    def __init__(self, *args, **kwargs):
        ...
        
    def get_memory(self):
        mem = psutil.virtual_memory()
        zj = float(mem.total) / 1024 / 1024 / 1024
        ysy = float(mem.used) / 1024 / 1024 / 1024
        kx = float(mem.free) / 1024 / 1024 / 1024
        print('系统总计内存:%d.4GB' % zj)
        print('系统已经使用内存:%d.4GB' % ysy)
        print('系统空闲内存:%d.4GB' % kx)
        
    def get_cpu(self):
        print(psutil.cpu_times(percpu=True))
        print(u'逻辑CPU个数:%d' % psutil.cpu_count())
        print(u'物理CPU个数:%d' % psutil.cpu_count(logical=False))
        cpu = str(psutil.cpu_percent(1)) + '%'
        print(u'cpu使用率:%s' % cpu)
        
    def get_disk(self):
        part = psutil.disk_partitions()
        for i in part:
            print(i)
        dk = psutil.disk_usage('/')
        print(dk)
        total = dk.total / 1024 / 1024 / 1024
        used = dk.used / 1024 / 1024 / 1024
        free = dk.free / 1024 / 1024 / 1024
        print('系统总计磁盘:%d.3GB' % total)
        print('系统已经使用磁盘:%d.3GB' % used)
        print('系统空闲磁盘:%d.3GB' % free)
        print(u"磁盘使用率: %s%%" % dk.percent)
        print(psutil.disk_io_counters())
        
    def get_net(self):
        print(psutil.net_io_counters())
        # 发送数据包
        print("发送数据字节:", psutil.net_io_counters().bytes_sent,"bytes")
        #接收数据包
        print("接收数据字节:",psutil.net_io_counters().bytes_recv,"bytes")
        net_counter = psutil.net_io_counters(pernic=True)
        for i in net_counter:
            print("网卡："+i+" ,网卡信息：",net_counter[i])
        
    def get_netspeed(self):
        s1 = psutil.net_io_counters(pernic=True)['WLAN']
        time.sleep(1)
        s2 = psutil.net_io_counters(pernic=True)['WLAN']
        result = s2.bytes_recv - s1.bytes_recv
        print(str('%d' % (result / 1024)) + 'kb/s')
    
    def get_time(self):
        print(datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H: %M: %S"))
    
    def get_user(self):
        users = psutil.users()
        for i in users:
            print(i)
        
        
if __name__ == "__main__":
    system_monitoring = SystemMonitoring()
    # system_monitoring.get_memory()
    # system_monitoring.get_cpu()
    # system_monitoring.get_disk()
    # system_monitoring.get_net()
    # system_monitoring.get_netspeed()
    # system_monitoring.get_time()
    system_monitoring.get_user()